package com.ruoyi.report.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.report.mapper.CommunityReportingMapper;
import com.ruoyi.report.domain.CommunityReporting;
import com.ruoyi.report.service.ICommunityReportingService;

import static com.ruoyi.common.utils.DateUtils.YYYY_MM_DD_HH_MM_SS;
import static com.ruoyi.common.utils.SecurityUtils.getUserId;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 档案Service业务层处理
 *
 * @author mac
 * @date 2023-02-11
 */
@Service
public class CommunityReportingServiceImpl implements ICommunityReportingService {
    @Autowired
    private CommunityReportingMapper communityReportingMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysUserRoleMapper roleMapper;

    /**
     * 查询档案
     *
     * @param id 档案主键
     * @return 档案
     */
    @Override
    public CommunityReporting selectCommunityReportingById(Long id) {
        return communityReportingMapper.selectCommunityReportingById(id);
    }

    /**
     * 查询档案列表
     *
     * @param communityReporting 档案
     * @return 档案
     */
    @Override
    public List<CommunityReporting> selectCommunityReportingList(CommunityReporting communityReporting) {
        Long userId = getUserId();
        Long roleId = roleMapper.selectRoleByUserId(userId);
        String username = getUsername();
        if (roleId != 1 && roleId != 100) {
            communityReporting.setUserName(username);
            return communityReportingMapper.selectCommunityReportingList(communityReporting);
        }
        return communityReportingMapper.selectCommunityReportingList(communityReporting);
    }

    /**
     * 新增档案
     *
     * @param communityReporting 档案
     * @return 结果
     */
    @Override
    public int insertCommunityReporting(CommunityReporting communityReporting) {
        if (ObjectUtils.isEmpty(userMapper.selectUserByUserName(communityReporting.getUserName()))) {
            throw new RuntimeException("用户不存在！");
        }
        return communityReportingMapper.insertCommunityReporting(communityReporting);
    }

    /**
     * 修改档案
     *
     * @param communityReporting 档案
     * @return 结果
     */
    @Override
    public int updateCommunityReporting(CommunityReporting communityReporting) {
        if (ObjectUtils.isEmpty(userMapper.selectUserByUserName(communityReporting.getUserName()))) {
            throw new RuntimeException("用户不存在！");
        }
        return communityReportingMapper.updateCommunityReporting(communityReporting);
    }

    /**
     * 批量删除档案
     *
     * @param ids 需要删除的档案主键
     * @return 结果
     */
    @Override
    public int deleteCommunityReportingByIds(Long[] ids) {
        return communityReportingMapper.deleteCommunityReportingByIds(ids);
    }

    /**
     * 删除档案信息
     *
     * @param id 档案主键
     * @return 结果
     */
    @Override
    public int deleteCommunityReportingById(Long id) {
        return communityReportingMapper.deleteCommunityReportingById(id);
    }

    @Override
    public CommunityReporting selectCommunityReportingByNameAndMemo(String userName, String xueQi) {

        return communityReportingMapper.selectCommunityReportingByNameAndMemo(userName, xueQi);
    }
}
